﻿using App.Extension;
using Simple.BasicNet.Core;
using Simple.BasicNet.Core.Atrributes;
using Simple.BasicNet.Core.Net;
using Simple.BasicNet.Core.Schedule;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/*********************************************************
 * 命名空间 App.Schedule
 * 接口名称 CheckSchedule
 * 开发人员：11920
 * 创建时间：2023/4/11 11:31:31
 * 描述说明：
 * 更改历史：
 * 
 * *******************************************************/
namespace App.Schedule
{
	internal class CheckHeartSchedule : ISchedule
	{
		private ILogger logger;
		public CheckHeartSchedule(IClientManager clientManager, ILogger logger)
		{
			ScheduleInfo = new ScheduleInfo();
			ScheduleInfo.Period = 10000;
			ScheduleInfo.BeforeStartWait = 1000*60*1;//等待一分钟
			this.clientManager = clientManager;
			this.logger = logger;
		}
		private IClientManager clientManager;
		public ScheduleInfo ScheduleInfo { get { return scheduleInfo; }set {scheduleInfo=value; } }
		public void Run()
		{

			//logger.Info($"心跳包:{DateTime.Now}");
			//logger.Info($"次数:{count}");
			logger.Info($"数量:{clientManager.GetClientCount()}");
			foreach (var item in clientManager.GetClients())
			{
				if (item.Context is IDTContext idtContext)
				{
					if (idtContext.CheckHeartIsPast())
					{
						logger.Info($"########ID:{idtContext.Device.SN}120秒没有应答!");
						clientManager.RemoveClient(item.ConnectionID);
					}
				}
			}
			count++;
		}
		private ScheduleInfo scheduleInfo;
		private int count;
	}
}
